const path = require('path')
const Koa = require('koa')
const cors = require('koa2-cors')
const KoaBody = require('koa-body')
const KaoStatic = require('koa-static')
const errorHandler = require('./errHandler')
const router = require('../router/index')


const app = new Koa()
app.use(cors());
app.use(KoaBody({
    multipart: true,
    formidable: {
        uploadDir: path.join(__dirname,'../upload'),
        keepExtensions: true
    }
}
))
app.use(KaoStatic(path.join(__dirname,'../upload')))
app.use(router.routes()).use(router.allowedMethods())

// 统一错误处理
app.on('error',errorHandler)
module.exports = app